package com.shanghaionstar.utils;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Vibrator;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.PreferencesUtls;

/**
 * 
* @ClassName: PlaySoundUtil 
* @Description: play remote command result voice
* @author 6335yz
* @date 2015年9月17日 上午10:19:04
 */
public class PlaySoundUtil {
	private static Ringtone alarmRingtone;
	
	/**
	 * 
	* @Title: playDefaultSound 
	* @Description: 播放系统自带声音 isVibartor 是否震动 true|false  return 是|否
	* @param @param context
	* @param @param isVibartor    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public static void playDefaultSound(Context context, boolean isVibartor) {
		if (getIsPlaySound(context)) {
			alarmRingtone = getSingleAlarmRingtone(context);
			if (alarmRingtone != null && !alarmRingtone.isPlaying()) {
				alarmRingtone.play();
			}
		}
		if (isVibartor) {
			isVibartor(context);
		}
	}

	/**
	 * 
	* @Title: vibartor 
	* @Description: vibartor
	* @param @param context    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public static void vibartor(Context context) {
		isVibartor(context);
	}
	
	/**
	 * 
	* @Title: getOvdAutoRefresh 
	* @Description: 是否登陆时自动更新车况数据
	* @param @param context    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public static boolean getOvdAutoRefresh(Context context) {
		return getIsOvdAutoRefresh(context);
	}
	
	/**
	 * 
	* @Title: getSingleAlarmRingtone 
	* @Description: 构造一个单例，避免重复播放
	* @param @param context
	* @param @return    设定文件 
	* @return Ringtone    返回类型 
	* @throws
	 */
	private static Ringtone getSingleAlarmRingtone(Context context) {
		if (alarmRingtone == null) {
			alarmRingtone = RingtoneManager.getRingtone(context,
					RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
		}
		return alarmRingtone;
	}

	
	public static void playCustomSound(Context context, boolean isVibartor, int rawId) {

		if (rawId == 0) {
			playDefaultSound(context, isVibartor);
			return;
		}
		if (isVibartor) {
			isVibartor(context);
		}
		if (getIsPlaySound(context)) {
			playSound(context, rawId);
		}
	}

	private static void playSound(Context context, int rawId) {
		AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		if (am.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
			return;
		}
		try {
			MediaPlayer mediaPlayer = MediaPlayer.create(context, rawId);
			mediaPlayer.start();
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					mp.release();
					mp = null;
				}
			});
		} catch (Exception e) {
			LogManager.getInstance().e(e.getMessage());
		}
	}

	/**
	 * 
	* @Title: playCustomSound 
	* @Description: 播放默认自带声音
	* @param @param context
	* @param @param isVibartor    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public static void playCustomSound(Context context, boolean isVibartor) {
		playDefaultSound(context, isVibartor);
	}

	/**
	 * 
	* @Title: isVibartor 
	* @Description: 是否需要震动
	* @param @param context    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	private static void isVibartor(Context context) {
		if (!getIsVibartor(context)) {
			return;
		}
		
		AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		if (am.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
			return;
		}
		Vibrator vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
		vibrator.vibrate(1000);
	}
	
	/**
	 * 
	* @Title: getIsPlaySound 
	* @Description: 是否播放声音
	* @param @param context
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	private static boolean getIsPlaySound(Context context) {
		return PreferencesUtls.getInstance(context).getBoolean(FinalConstants.SharedPreferencesConfig.IS_PALY_SOUND, true);
	}

	/**
	 * 
	* @Title: getIsVibartor 
	* @Description: 是否震动
	* @param @param context
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	private static boolean getIsVibartor(Context context) {
		return PreferencesUtls.getInstance(context).getBoolean(FinalConstants.SharedPreferencesConfig.IS_VIBARTOR, true);
	}
	
	/**
	 * 
	* @Title: getIsOvdAutoRefresh 
	* @Description: 是否登陆时自动更新车况数据
	* @param @param context
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	private static boolean getIsOvdAutoRefresh(Context context) {
		return PreferencesUtls.getInstance(context).getBoolean(FinalConstants.SharedPreferencesConfig.OVD_AUTO_REFRESH, false);
	}
	
}
